package com.fengwan.wastesort.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.fengwan.wastesort.R
import com.fengwan.wastesort.model.Waste
import com.xuexiang.xui.utils.ResUtils
import kotlinx.android.synthetic.main.item_waste_header.view.*
import kotlinx.android.synthetic.main.item_waste_sort.view.*

/**
 * @author : Silence丶J
 * @time : 2019/7/18
 * @description :
 */
class WasteSortAdapter2(private val context: Context) :
    RecyclerView.Adapter<WasteSortAdapter2.ViewHolder>() {


    private var data: MutableList<Waste> = mutableListOf<Waste>()
    private val HEADER_TYPE = 0
    private val CONTENT_TYPE = 1

    private @ColorRes
    var header_view_color: Int = 0
    private var header_view_title: String = ""
    private var header_view_title_image = 0
    private var header_view_sub_title = ""

    fun setNewData(data: List<Waste>) {
        this.data.clear()
        this.data.addAll(data)
        notifyDataSetChanged()
    }

    fun addData(data: List<Waste>) {
        val old = this.data.size
        this.data.addAll(data)
        notifyItemRangeChanged(old, this.data.size)
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> {
                HEADER_TYPE
            }
            else -> {
                CONTENT_TYPE
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View = when (viewType) {
            HEADER_TYPE -> {
                LayoutInflater.from(context).inflate(R.layout.item_waste_header, parent, false)
            }
            CONTENT_TYPE -> LayoutInflater.from(context).inflate(R.layout.item_waste_sort, parent, false)
            else -> {
                LayoutInflater.from(context).inflate(R.layout.item_waste_sort, parent, false)
            }
        }
        return ViewHolder(view)
    }

    fun setHeaderView(view: View) {
    }

    fun setHeaderData(
        @ColorRes viewBgColor: Int,
        title: CharSequence, @DrawableRes titleImage: Int,
        subtitle: CharSequence
    ) {
        this.header_view_color = viewBgColor
        this.header_view_title = title.toString()
        this.header_view_title_image = titleImage
        this.header_view_sub_title = subtitle.toString()
    }

    override fun getItemCount(): Int {
        return (data.size + 1) ?: 0
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        when (position) {
            0 -> {
                if (header_view_color != 0 &&
                    !header_view_title.isNullOrEmpty() &&
                    header_view_title_image != 0 &&
                    !header_view_sub_title.isNullOrBlank()
                ) {
                    holder.itemView.layoutParams = RecyclerView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                    )
                    holder.itemView.card_view.setCardBackgroundColor(ResUtils.getColor(header_view_color))
                    holder.itemView.tv_title.text = header_view_title
                    holder.itemView.tv_title.setCompoundDrawablesWithIntrinsicBounds(
                        null,
                        null,
                        ResUtils.getDrawable(header_view_title_image),
                        null
                    )
                    holder.itemView.tv_subtitle.text = header_view_sub_title
                }
            }
            else -> {
                holder.itemView.item_tv.text = getItem(position)?.name
            }
        }
    }

    fun getItem(position: Int): Waste? {
        return data.get(position - 1)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val manager = recyclerView.layoutManager
        if (manager != null && manager is GridLayoutManager) {
            val m = manager as GridLayoutManager
            m.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return when (position) {
                        0 -> {
                            m.spanCount
                        }
                        else -> {
                            1
                        }
                    }
                }

            }
        }
    }


    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}